home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
ICON_8
/
ICONX_FO
/
RSYS.C
< prev
Wrap
Text File
|
1990-03-02
|
4KB
|
211 lines
/*
* File: rsys.c
* Contents: getstrg, host, longread, putstr
*/
#include "::h:config.h"
#include "::h:rt.h"
#include "rproto.h"
#if AMIGA
#if LATTICE
#include <ios1.h>
#endif /* LATTICE */
#endif /* AMIGA */
/*
* getstrg - read a line into buf from file fd. At most maxi characters
* are read. getstrg returns the length of the line, not counting
* the newline. Returns -1 if EOF and -2 if length was limited by
* maxi. [[ Needs ferror() check. ]]
*/
int getstrg(buf, maxi, fd)
register char *buf;
int maxi;
FILE *fd;
{
register int c, l;
#if AMIGA
#if LATTICE
/* This code is special for Lattice 4.0. It was different for
* Lattice 3.10 and probably won't work for other C compilers.
*/
extern struct UFB _ufbs[];
if (IsInteractive(_ufbs[fileno(fd)].ufbfh))
return read(fileno(fd),buf,maxi);
#endif /* LATTICE */
#endif /* AMIGA */
l = 0;
while ((c = fgetc(fd)) != '\n') {
if (c == EOF)
if (l > 0) return l;
else return -1;
if (++l > maxi) {
ungetc(c, fd);
return -2;
}
*buf++ = c;
}
return l;
}
#ifdef UtsName
#include <sys/utsname.h>
#endif /* UtsName */
/*
* iconhost - return some sort of host name into the buffer pointed at
* by hostname. This code accommodates several different host name
* fetching schemes.
*/
novalue iconhost(hostname)
char *hostname;
{
#ifdef WhoHost
/*
* The host name is in /usr/include/whoami.h. (V7, 4.[01]bsd)
*/
whohost(hostname);
#endif /* WhoHost */
#ifdef UtsName
{
/*
* Use the uname system call. (System III & V)
*/
struct utsname utsn;
uname(&utsn);
strcpy(hostname,utsn.nodename);
}
#endif /* UtsName */
#ifdef GetHost
/*
* Use the gethostname system call. (4.2bsd)
*/
gethostname(hostname,MaxCvtLen);
#endif /* GetHost */
#if VMS
/*
* VMS has its own special logic.
*/
char *h;
if (!(h = getenv("ICON$HOST")) && !(h = getenv("SYS$NODE")))
h = "VAX/VMS";
strcpy(hostname,h);
#endif /* VMS */
#ifdef HostStr
/*
* The string constant HostStr contains the host name.
*/
strcpy(hostname,HostStr);
#endif /* HostStr */
}
#ifdef WhoHost
#define HdrFile "/usr/include/whoami.h"
/*
* whohost - look for a line of the form
* #define sysname "name"
* in HdrFile and return the name.
*/
novalue whohost(hostname)
char *hostname;
{
char buf[BUFSIZ];
FILE *fd;
fd = fopen(HdrFile, "r");
if (fd == NULL) {
sprintf(buf, "Cannot open %s, no value for &host\n", HdrFile);
syserr(buf);
}
for (;;) { /* each line in the file */
if (fgets(buf, sizeof buf, fd) == NULL) {
sprintf(buf, "No #define for sysname in %s, no value for &host\n",
HdrFile);
syserr(buf);
}
if (sscanf(buf,"#define sysname \"%[^\"]\"", hostname) == 1) {
fclose(fd);
return;
}
}
}
#endif /* WhoHost */
/*
* Read a long string in shorter parts. (Standard read may not handle long
* strings.)
*/
word longread(s,width,len,fname)
FILE *fname;
int width;
char *s;
long len;
{
long tally = 0;
long n = 0;
while (len > 0) {
n = fread(s, width, (int)((len < MaxIn) ? len : MaxIn), fname);
if (n <= 0)
return tally;
tally += n;
s += n;
len -= n;
}
return tally;
}
/*
* Print string referenced by descriptor d. Note, d must not move during
* a garbage collection.
*/
int putstr(f, d)
register FILE *f;
dptr d;
{
register char *s;
register word l;
l = StrLen(*d);
if (l == 0)
return Success;
s = StrLoc(*d);
#ifdef FixedRegions
if (longwrite(s,l,f) < 0)
return Failure;
else
return Success;
#else /* FixedRegions */
/*
* In expandable regions storage management, the first output to a file may
* cause allocation, which in turn may cause a garbage collection, changing
* where the string is. So write one character and reload the address
* of the string from the tended descriptor.
*/
putc(*s, f);
s = StrLoc(*d) + 1;
if (longwrite(s,--l,f) < 0)
return Failure;
else
return Success;
#endif /* FixedRegions */
}